package com.spzx.channel.configure;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

/**
 * 初始化线程池
 */
@Slf4j
@Configuration
public class ThreadPoolConfig {

    @Bean
    public ThreadPoolExecutor threadPoolExecutor(){
        int processors = Runtime.getRuntime().availableProcessors(); //获取CPU核数
        // CPU密集型  IO密集型
        //面试题：线程池工作原理？
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                processors*2,
                processors*2,
                0, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(200),
                Executors.defaultThreadFactory(),
                (runnable, executor) -> {
                    log.error("线程池达到最大饱和策略");
                }
        );
        threadPoolExecutor.prestartAllCoreThreads();
        return threadPoolExecutor;
    }

}
